const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CleanWebpackPlugin = require('clean-webpack-plugin')
const ExtractTextWebpackPlugin = require('extract-text-webpack-plugin')

console.log('111111111111111111111111')
console.log(process.env.NODE_ENV)
console.log('222222222222222222222222222')


module.exports = {
    entry: path.resolve(__dirname, './src/App.jsx'),
    output: {
        filename: 'js/[name].[hash:8].js',
        path: path.resolve(__dirname, 'dist'),
        publicPath: process.env.NODE_ENV === 'production' ? './' : '/'
    },
    resolve: {
        extensions: ['.js', '.jsx'],
        alias: {
            "@component": path.resolve(__dirname, './src/component'),
            "@page": path.resolve(__dirname, './src/page'),
            "@service": path.resolve(__dirname, './src/service'),
            "@util": path.resolve(__dirname, './src/util'),
            "@static": path.resolve(__dirname, './src/static')
        }
    },
    devtool: 'eval-source-map',
    module: {
        rules: [{
                test: /\.(js|jsx)$/,
                exclude: /(node_modules)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['react', 'env'],
                        plugins: ['react-hot-loader/babel', ['import', {
                            libraryName: 'antd',
                            style: 'css'
                        }]]
                    }
                }
            },
            {
                test: /\.css$/,
                include: /(src)/,
                use: ExtractTextWebpackPlugin.extract({
                    fallback: "style-loader",
                    use: {
                        loader: "css-loader",
                        options: {
                            modules: true
                        }
                    }
                })
            },
            {
                test: /\.css$/,
                exclude: /(src)/,
                use: ExtractTextWebpackPlugin.extract({
                    fallback: "style-loader",
                    use: "css-loader"
                })
            },
            {
                test: /\.(png|jpg|gif)$/,
                use: [{
                    loader: 'url-loader',
                    options: {
                        limit: 8192,
                        name: 'images/[name].[ext]'
                    }
                }]
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            filename: 'index.html',
            template: './src/index.html'
        }),
        new CleanWebpackPlugin(['dist']),
        new ExtractTextWebpackPlugin("css/[name].css")
    ],
    devServer: {
        contentBase: path.join(__dirname, "dist"),
        historyApiFallback: true,
        compress: true,
        port: 9000
    }
}